public class class01 {
    private int num = 0;
    private char ch = 'a';

    private int flag = 1;

    public  void printNum() throws InterruptedException {
        synchronized (this) {
            while (flag != 1) {
                this.wait();
            }

            System.out.print(num++);
            flag = 2;
            this.notifyAll();
        }
    }

    public void printChar() throws InterruptedException {
        synchronized (this){
            while(flag != 2){
                this.wait();
            }
            //2、打印字符
            System.out.print(ch++);

            flag = 1; // 修改标志位，让其他线程人知道该打印数字了
            //3、通知其他线程
            this.notifyAll();
        }
    }

}
